/**
 * @author {Yuntao Zhou}
 */
var SettingPlugin=function(container,config,callback){
    this.container = container[0] ? container : $(container);
    var qslang=QS_Lang.workspace;
    this.config={
        width:180,
        showLockCheckable:true,
        showHideCheckable:true,
        isSelectAuto:true,
        isCheckAutoAddSymbol:true,
        isCheckLock:false,
        isShowHideChecked:true,
		description:'',
		showTabRadio:false,
		isSelectTabName:false,
		tabRadioTxt:qslang["win_rdefault"]
    };
    $.extend(this.config,config);
    this.title=this.config.title||qslang["win_dtitle"];
    this.isSelectAuto=this.config.isSelectAuto;
    this.isCheckLock=this.config.showLockCheckable&&this.config.isCheckLock;
    this.isCheckAutoAddSymbol=this.config.isCheckAutoAddSymbol;
    this.isShowHideChecked =this.config.showHideCheckable&&this.config.isShowHideChecked;
	this.isSelectTabName=this.config.showTabRadio&&this.config.isSelectTabName;
    this.callback={
        onCheckLock:null,//function(isLock){}
        onChangeTitle:null,//function(title,isAddSymbol,isAuto){}
        onShowHide:null
    };
    $.extend(this.callback,callback);
    this.el=null;
    this.chkAddSymbol=null;
    this.chkLock=null;
    this.chkShowHide =null;
    
	var description=this.config.description==''?String.format('<span>{0}</span> <span style="color:#acacac;">({1})</span>',qslang["win_rauto"],qslang["win_rautoeg"]):this.config.description;
    this.html='<div class="rtq-setting-plugin"><div class="clearfix block"><h4>'+qslang["win_setplugintitle"] +'</h4><div class="clearfix row"><a class="rtq-sp-radio rtq-sp-radio-on '+(this.isSelectAuto?'':'rtq-sp-radio-off')+'" value="auto">'+description+'</a></div>'+
	          (this.config.showTabRadio?('<div class="clearfix row"><a class="rtq-sp-radio rtq-sp-radio-on '+(this.isSelectTabName?'':'rtq-sp-radio-off')+'" value="tabName"><span>'+this.config.tabRadioTxt+'</span></a></div>'):'')+
              '<div class="clearfix row"><a class="rtq-sp-radio rtq-sp-radio-on '+(this.isSelectAuto||this.isSelectTabName?'rtq-sp-radio-off':'')+'" value="custom"><input class="rtq-sp-box qs-ui-ipt" type="text"/><div class="rtq-sp-box rtq-sp-btn">'+qslang["win_bnamesave"]+'</div><div class="chk-auto-symbol" style="display:none;"></div></a></div></div>'+
              (this.config.showLockCheckable?'<div class="clearfix block"><h4>Locking</h4><div class="lock"></div></div>':'')+
              (this.config.showHideCheckable?'<div class="clearfix block"><div class="tab-show-or-hide-wrap"></div></div>':'')+
              '</div>'
    this.init();
};
SettingPlugin.prototype={
    init:function(){
        var self=this;
        this.el=$(this.html).css('width',this.config.width).appendTo(this.container);
        if(this.config.className){
            this.el.addClass(this.config.className);
        }
        this.el.find('input').css('width',this.config.width-100);
        this.chkAddSymbol=new Util.ButtonGroup(this.el.find('.chk-auto-symbol'), [{txt:QS_Lang.workspace["win_bgaddsymbol"],val:'autoAddSymbol'}], {
            onClick: function(v, btn){
                self.isCheckAutoAddSymbol=btn.isSelected;
            }
        },{type:'checkbox'});
        if(this.isCheckAutoAddSymbol){
            this.chkAddSymbol.btns['autoAddSymbol'].select();
        }
        if (this.config.showLockCheckable) {
            this._createLockCheck();
        }
        if (this.config.showHideCheckable) {
            this._createShowHideTabCheck(true);
        }
        this._bindEvent();
    },
    _createLockCheck:function(){
        var self=this;
        this.chkLock = new Util.ButtonGroup(this.el.find('.lock'), [{txt:QS_Lang.workspace["win_clockwin"],val:'lock'}], {
            onClick: function(v, btn){
                self.isCheckLock=btn.isSelected;
                if(typeof self.callback.onCheckLock=='function'){
                    self.callback.onCheckLock(btn.isSelected);
                }
            }
        },{type:'checkbox'});
        this.chkLock.el[0].title = QS_Lang.workspace["win_funtitle"];
        if(this.isCheckLock){
            this.chkLock.btns['lock'].select();
        }
    },
    _createShowHideTabCheck:function(flag){
        var self=this;
        this.chkShowHide = new Util.ButtonGroup(this.el.find('.tab-show-or-hide-wrap'), [{txt:QS_Lang.workspace["win_cshowtool"],val:'show'}], {
            onClick: function(v, btn){
                self.isShowHideChecked = btn.isSelected;
                if(typeof self.callback.onShowHide=='function'){
                    self.callback.onShowHide(btn.isSelected);
                }
                //self.changeOutSize();
            }
        },{type:'checkbox'});
        this.callback.onShowHide(this.isShowHideChecked,flag);
        if(this.isShowHideChecked){
            this.chkShowHide.btns['show'].select();
        }
    },
    _bindEvent:function(){
        var self=this;
        var $radio=this.el.find('.rtq-sp-radio');
        var $input=this.el.find('.qs-ui-ipt');
        var $btn=this.el.find('.rtq-sp-btn');
        $radio.bind('click',function(e){
            var target=$(e.target);
            if (target.hasClass('rtq-sp-radio')) {
                var value = target.attr('value');
                $radio.addClass('rtq-sp-radio-off');
                target.removeClass('rtq-sp-radio-off');
				var isExecCallback=false;
				if(value=='auto'){
					self.isSelectAuto=true;
					self.isSelectTabName = false;
					isExecCallback=true;
				}else if(value=='tabName'){
					if(self.config.showTabRadio){
						self.isSelectAuto=false;
                        self.isSelectTabName = true;
						isExecCallback=true;
					}
				}
				if(isExecCallback){
					$input.val('');
                    if (typeof self.callback.onChangeTitle == 'function') {
                        self.callback.onChangeTitle(null, self.isCheckAutoAddSymbol, self.isSelectAuto, self.isSelectTabName);
                    }
				}
            }
        });
        
        $input.bind('click',function(e){
            $radio.addClass('rtq-sp-radio-off');
			var index=1;
			if(self.config.showTabRadio){
				index=2;
			}
            $radio.eq(index).removeClass('rtq-sp-radio-off');           
        });
        
        $btn.bind('click',function(e){			
            if($input.val()==''){
                return;
            }
			if(!QS_Validator.test($input.val(),'name')){
	            return;
	        }
			self.isSelectAuto=false;
            self.isSelectTabName=false;
            self.title=$input.val();
            if(typeof self.callback.onChangeTitle=='function'){
                self.callback.onChangeTitle(self.title,self.isCheckAutoAddSymbol,self.isSelectAuto,self.isSelectTabName);
            }
        });
    },
    disable:function(el,isDiasble){
        if(isDiasble){
            el.addClass('rtq-btn-disable');
        }else{
            el.removeClass('rtq-btn-disable');
        }
    },
	setName:function(isAuto,name){
		this.isSelectAuto=isAuto;
		var $radio=this.el.find('.rtq-sp-radio')
		if(isAuto){
		   $radio.addClass('rtq-sp-radio-off');
		   $radio.eq(0).removeClass('rtq-sp-radio-off');	   
		}else{
		   $radio.addClass('rtq-sp-radio-off');
           var index=1;
		   if(this.config.showTabRadio){
		   	  index=2;
		   }
		   $radio.eq(index).removeClass('rtq-sp-radio-off');
		}
		if (typeof name != 'undefined' && name != '') {
			this.el.find('.qs-ui-ipt').val(name);
		}
	},
    saveConfig:function(){
       var config={
            isSelectAuto:this.isSelectAuto,
            isCheckLock:this.isCheckLock,
            isCheckAutoAddSymbol:this.isCheckAutoAddSymbol,
            isShowHideChecked:this.isShowHideChecked
        };
		if(this.config.showTabRadio){
			config.isSelectTabName=this.isSelectTabName;
		}
		return config;
    }
};

